package com.dfs.parent.common.core.domain;

import com.dfs.parent.common.core.enums.IErrorCode;
import com.dfs.parent.common.core.enums.ResultCode;
import lombok.Getter;

import java.io.Serializable;

/**
 * 接口返回结果
 *
 * @author dahong
 * @date 2019年7月19日 下午5:05:10
 */
@Getter
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private int code;
    private String msg;
    private T data;

    public Result() {
    }
    public Result(int code) {
        this.code = code;
    }
    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static <T> Result<T> ok() {
        return new Result<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
    }

    public static <T> Result<T> fail() {
        return new Result<T>(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMsg());
    }

    public static <T> Result<T> ok(T data) {
        return new Result<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    public static <T> Result<T> fail(int code) {
        return new Result<T>(code,ResultCode.FAIL.getMsg());
    }
    public static <T> Result<T> fail(String msg) {
        return new Result<T>(ResultCode.FAIL.getCode(),msg);
    }
    public static <T> Result<T> fail(int code, String msg) {
        return new Result<T>(code, msg);
    }
    public static <T> Result<T> fail(int code, String msg,T data) {
        return new Result<T>(code, msg,data);
    }
    public static boolean isSuccess(Result<?> result) {
        return result != null && ResultCode.SUCCESS.getCode().equals(result.getCode());
    }
    public static <T> Result<T> fail(IErrorCode errorCode) {
        return new Result<T>(errorCode.getCode(),errorCode.getMsg());
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result setCode(int code) {
        this.code = code;
        return this;
    }

    public Result setData(T data) {
        this.data = data;
        return this;
    }
    
}
